package core.proto.timeline;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;

import de.jaret.util.ui.timebars.model.TimeBarRowHeader;
import de.jaret.util.ui.timebars.swing.TimeBarViewer;
import de.jaret.util.ui.timebars.swing.renderer.HeaderRenderer;

/**
 * Modified from: 
 * {@link de.jaret.examples.timebars.eventmonitoring.swing.renderer.EventMonitorHeaderRenderer}
 * 
 * A header renderer that renders the label always on top of the supplied space and 
 * doing a gradient fill of the background.
 * 
 * @author Wei
 */
public class SavantHeaderRenderer implements HeaderRenderer {
    private HeaderComponent _headerComponent;

    private final int WIDTH = 100;
    
    public SavantHeaderRenderer() {
        _headerComponent = new HeaderComponent();
    }

    /**
     * {@inheritDoc}
     */
    public JComponent getHeaderRendererComponent(TimeBarViewer tbv, TimeBarRowHeader value, boolean isSelected) {
        _headerComponent.setSelected(isSelected);
        _headerComponent.setTimeBarRowHeader(value);
        _headerComponent.setTimeBarViewer(tbv);
        return _headerComponent;
    }

    /**
     * {@inheritDoc}
     */
    public int getWidth() {
        return WIDTH;
    }

    /**
     * The Component to be painted as the header.
     * This class based on the one found in the jaret timeline example. 
     */
    public class HeaderComponent extends JComponent {
        private final static int MARGIN = 3;

        private TimeBarRowHeader _header;
        private boolean _selected;
        private TimeBarViewer _tbv;

        public HeaderComponent() {
            setLayout(null);
            setOpaque(false);
        }

        public void setTimeBarViewer(TimeBarViewer tbv) {
            _tbv = tbv;
        }

        public void setTimeBarRowHeader(TimeBarRowHeader header) {
            _header = header;
        }

        public String getToolTipText() {
            return "<html><b>" + _header.getLabel() + "</b></html>";
        }

        public void setSelected(boolean selected) {
            _selected = selected;
        }

        /**
         * {@inheritDoc} The header renderer tries to get as much space as possible.
         */
        public Dimension getPreferredSize() {
            return new Dimension(100, 1000);
        }

        protected void paintComponent(Graphics graphics) {
            super.paintComponent(graphics);
            int height = getHeight();
            int width = getWidth();

            // Colors
            int startGray = 200;
            int endGray = 230;
            Color sColor = new Color(startGray, startGray, startGray);
            Color eColor = new Color(endGray, endGray, endGray);

            // background plain fill
            graphics.setColor(Color.RED);
            graphics.fillRect(0, 0, width, height);

            // background gradient fill
            Graphics2D g2 = (Graphics2D) graphics.create();
            GradientPaint gradientPaint = new GradientPaint(width / 2, 0, sColor, width / 2, height / 2, eColor, false);
            g2.setPaint(gradientPaint);
            g2.fillRect(0, 0, width, height);

            graphics.setColor(Color.BLACK);

            // row grid
            if (_tbv.getDrawRowGrid()) {
                graphics.drawLine(0, height - 1, width, height - 1);
            }

            // text
            Rectangle2D rect = graphics.getFontMetrics().getStringBounds("Q", graphics);
            int textHeight = (int) rect.getHeight();
            graphics.drawString(_header.getLabel(), MARGIN, MARGIN + textHeight);

            // selection
            if (_selected) {
                graphics.setColor(Color.BLUE);
                g2 = (Graphics2D) graphics;
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f));
                graphics.fillRect(0, 0, width, height);
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
            }
        }
    }
}
